package com.cooode.web.controller;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cooode.common.uitls.SecurityUtils;
import com.cooode.framework.web.controller.BaseController;
import com.cooode.framework.web.entity.ResultData;
import com.cooode.web.entity.SysUser;
import com.cooode.web.service.ISysUserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;

/**
 * @desc 用户信息控制类
 * @author tianmaochang
 * @date 2024-08-15
 */
@RestController
@RequestMapping("/system/user")
public class SysUserController extends BaseController {

    @Resource
    ISysUserService sysUserService;

    /***
     * 根据用户名查询用户信息
     * @param sysUser
     * @return
     */
    @GetMapping("/list")
    public ResultData list(@Param("sysUser") SysUser sysUser){
        List<SysUser> sysUserList = sysUserService.selectList(sysUser);
        ResultData resultData = ResultData.success();
        resultData.put("list", sysUserList);
        resultData.put("total", sysUserList.size());
        return resultData;
    }

    @GetMapping("/page")
    public IPage<SysUser> page(@Param("pageNum") Integer pageNum, @Param("pageSize") Integer pageSize, @RequestBody SysUser sysUser){
        return sysUserService.page(pageNum, pageSize, sysUser);
    }

    /***
     * 根据用户名查询用户信息
     * @param username
     * @return
     */
    @GetMapping("/name")
    public ResultData getByUsername(@Param("username") String username){
        SysUser sysUser = sysUserService.selectByUserName(username);
        return ResultData.success(sysUser);
    }

    /**
     * 新增用户
     * @param user
     * @return
     */
    @PostMapping("/add")
    public ResultData add(@Validated @RequestBody SysUser user) {
        user.setCreateBy(SecurityUtils.getUsername());
        user.setPassword(SecurityUtils.encryptPassword(user.getPassword()));
        return numResultData(sysUserService.insertUser(user));
    }
}
